<template>
	<view class="container">
		<NavBar title="我的发帖"></NavBar>
		<view class="postWrap" v-if="!empty">
			<view class="postCard" v-for="(item,index) of newPostList" :key="item._id">
				<view class="top">
					<u-tag :text="item.catalog" shape="circle" size="mini" type="success"></u-tag>
					<text class="title">{{item.title}}</text>
					<text class="created">{{item.created}}</text>
				</view>
				<view class="bottom">
					<view class="btnWrap">
						<u-button type="success" plain text="查看" shape="circle" size="mini" @click="toPostDetail(item._id)"></u-button>
					</view>
					<view class="btnWrap">
						<u-button  color="#ffaa00" plain text="编辑" shape="circle" size="mini" @click="toAddPost(item)"></u-button>
					</view>
					<view class="btnWrap">
						<u-button type="error" plain text="删除" shape="circle" size="mini" @click="deletePost(item._id)"></u-button>
					</view>
				</view>
			</view>
		</view>
		<u-empty
			v-else
			marginTop="140" 
			text="您还未发表文章" 
			icon="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-8da6cc70-751c-428d-9aa0-6516abc1603f/bd606095-2172-407e-b189-c6a5fbc14a59.png">
		</u-empty>
	</view>
</template>

<script>
	import moment from 'dayjs'
	export default {
		data() {
			return {
				empty:false,
				postList:[]
			};
		},
		onShow(){
			this.getMyPost()
		},
		computed:{
			newPostList () {
				const newPostList = this.postList.map(item => {
					item.created = moment(item.created).format('YYYY-MM-DD')
					switch (item.catalog) {
						case 'ask':
							item.catalog = '提问'
							break
						case 'share':
							item.catalog = '分享'
							break
						case 'notice':
							item.catalog = '公告'
						break
					}
					return item
				})
				return newPostList
			}
		},
		methods:{
			async getMyPost(){
				const {code,data} = await this.$axios.api.getMyPost()
				if(code === 200){
					this.postList = data
					data.length > 0?this.empty = false:this.empty = true
				}
			},
			toPostDetail (tid) {
				uni.navigateTo({
					url: `/subcom-home/PostDetail/PostDetail?tid=${tid}`
				})
			},
			toAddPost (data) {
				setTimeout(function () {
					uni.$emit('givePost', {...data,isEditPost:true})
				}, 200)
				uni.navigateTo({
					url: '/pages/AddPost/AddPost'
				})
			},
			deletePost (id) {
				const this_ = this
				uni.showModal({
					title: '提示',
					content: '确定要删除该帖吗?',
					success: function (res) {
						if (res.confirm) {
							this_.$axios.api.deletePost({
								id: id
							}).then(res => {
								if (res.code === 200) {
									uni.showToast({
										title: '删除成功',
										icon: 'success',
										duration: 2000
									})
									this_.getMyPost()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.container {
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	background-color: rgb(244, 244, 244);
	.postWrap {
		height: auto;
		padding: 20rpx;
		box-sizing: border-box;
		.postCard {
			height: 300rpx;
			border-radius: 20rpx;
			background-color: #fff;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding: 20rpx;
			box-sizing: border-box;
			margin-bottom: 20rpx;
			.top {
				height: 120rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				.title {
					font-size: 28rpx;
					font-weight: bold;
					max-width: 60%;
				}
				.created {
					font-size: 24rpx;
				}
			}
			.bottom {
				width: 100%;
				height: 60rpx;
				display: flex;
				justify-content: flex-end;
				align-items: center;
				.btnWrap{
					width: 140rpx;
					margin-left: 20rpx;
				}
			}
		}
	}
}
</style>
